생명게임 소스 :: 자바예제모음[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

자바예제모음
[1]
등록일:2008-04-15 17:00:01 (0%)
작성자:
제목:생명게임 소스

Image size : 208 Ⅹ 277


import  java.applet.*;
import  java.awt.*;
import  java.awt.event.*;

public  class  LifeGame  extends  Applet  implements  MouseListener,  Runnable  {
        private  int  xWidth  =  20;

        private  int  yHeight  =  20;

        private  int  xySize  =  10;

        private  boolean  map[][]  =  null;

        private  int  state[][]  =  null;

        private  Thread  thread  =  null;

        public  void  init()  {
                map  =  new  boolean[this.xWidth][this.yHeight];
                state  =  new  int[this.xWidth][this.yHeight];

                for  (int  i  =  0;  i  <  this.xWidth;  i++)  {
                        for  (int  j  =  0;  j  <  this.yHeight;  j++)  {
                                this.map[i][j]  =  false;
                                this.state[i][j]  =  0;
                        }
                }

                this.map[5][5]  =  true;
                this.map[6][5]  =  true;
                this.map[7][5]  =  true;
                this.map[8][5]  =  true;
                this.map[9][5]  =  true;
                this.map[10][5]  =  true;
                this.map[5][6]  =  true;
                this.map[6][6]  =  true;
                this.map[7][6]  =  true;
                this.map[8][6]  =  true;
                this.map[9][6]  =  true;
                this.map[10][6]  =  true;
                this.map[5][7]  =  true;
                this.map[6][7]  =  true;
                this.map[7][7]  =  true;
                this.map[8][7]  =  true;
                this.map[9][7]  =  true;
                this.map[10][7]  =  true;

                this.addMouseListener(this);

                this.thread  =  new  Thread(this);
                this.thread.start();
        }

        public  void  paint(Graphics  g)  {
                for  (int  i  =  0;  i  <  this.xWidth;  i++)  {
                        for  (int  j  =  0;  j  <  this.yHeight;  j++)  {
                                if  (map[i][j]  ==  false)  {
                                        g.drawRect(i  *  this.xySize,  j  *  this.xySize,  this.xySize,
                                                        this.xySize);
                                }  else  {
                                        g.fillRect(i  *  this.xySize,  j  *  this.xySize,  this.xySize,
                                                        this.xySize);
                                }
                        }
                }
        }

        public  void  run()  {
                while  (true)  {
                        try  {
                                Thread.sleep(1000);
                        }  catch  (Exception  e)  {
                                e.printStackTrace();
                        }

                        this.checkGen();
                        this.countGen();

                        for  (int  i  =  0;  i  <  this.xWidth;  i++)  {
                                for  (int  j  =  0;  j  <  this.yHeight;  j++)  {
                                        if  (this.state[i][j]  ==  3  ||  this.state[i][j]  ==  12
                                                        ||  this.state[i][j]  ==  13)  {
                                                this.map[i][j]  =  true;
                                        }  else  {
                                                this.map[i][j]  =  false;
                                        }
                                }
                        }

                        this.repaint();
                }
        }

        public  void  checkGen()  {
                //  각칸에  생명이  있는지  체크후  state  배열에  값을  넣는다.
                for  (int  i  =  0;  i  <  this.xWidth;  i++)  {
                        for  (int  j  =  0;  j  <  this.yHeight;  j++)  {
                                if  (map[i][j]  ==  true)  //  생명이  있는  경우
                                {
                                        this.state[i][j]  =  10;
                                }  else  {
                                        this.state[i][j]  =  0;
                                }
                        }
                }
        }

        public  void  countGen()  {
                //  각  칸에  생명이  몇개가  있는지  체크한다.
                for  (int  i  =  0;  i  <  this.xWidth;  i++)  {
                        for  (int  j  =  0;  j  <  this.yHeight;  j++)  {
                                this.state[i][j]  +=  returnBoolean(i  -  1,  j  -  1)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i  -  1,  j)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i  -  1,  j  +  1)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i,  j  -  1)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i,  j  +  1)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i  +  1,  j  -  1)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i  +  1,  j)  ?  1  :  0;
                                this.state[i][j]  +=  returnBoolean(i  +  1,  j  +  1)  ?  1  :  0;
                        }
                }
        }

        public  boolean  returnBoolean(int  x,  int  y)  {
                //  x,  y  위치에  생명이  있는지  체크후  boolean값을  반환한다.
                boolean  flag  =  false;

                if  (x  >  -1  &&  x  <  this.xWidth  &&  y  >  -1  &&  y  <  this.yHeight)  {
                        flag  =  map[x][y];
                }

                return  flag;
        }

        public  void  mouseClicked(MouseEvent  e)  {
                int  xPoint  =  e.getX()  /  10;
                int  yPoint  =  e.getY()  /  10;

                if  (this.map[xPoint][yPoint]  ==  true)  {
                        this.map[xPoint][yPoint]  =  false;
                }  else  {
                        this.map[xPoint][yPoint]  =  true;
                }

                this.repaint();
        }

        public  void  mouseEntered(MouseEvent  e)  {

        }

        public  void  mouseExited(MouseEvent  e)  {

        }

        public  void  mousePressed(MouseEvent  e)  {

        }

        public  void  mouseReleased(MouseEvent  e)  {

        }

        public  static  void  main(String[]  args)  {
        }
}

출처  :  http://elog.comrg.net/java/1203

무슨게임인지  잘  모르겠네요
[본문링크] 생명게임 소스
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=7412
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.